$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + '/user/searchUserInfo.do',
        datatype: "json",
        colModel: [
            { label: '客户编号', name: 'uid', index: "uid", width: 100, key: true },
            { label: '用户名称', name: 'loginname', index: "loginname", width: 90 },
            { label: '性别', name: 'gender', sortable: false, width: 90 },
            { label: '地址', name: 'address', width: 100 },
            { label: '手机号', name: 'phone', width: 80 },
            { label: '业务', name: 'businessType', width: 80 },
            { label: '备注', name: 'remark', width: 100 }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
        	 root: "page.list",
             page: "page.currPage",
             total: "page.totalPage",
             records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
    });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
        loginname:null,
        showList: true,
        title:null,
        user:{
        	uid:null,
        	loginname:null,
        	gender:null,
        	address:null,
        	phone:null,
        	businessType:null,
        	remark:null
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        update: function () {
            var uid = getSelectedRow();
            if(uid == null){
                return ;
            }
            vm.title = "修改";
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "/user/getUserByUid.do?uid="+uid,
                contentType: "application/json",
                success: function(r){
                    if(r.code === 0){
                    	vm.user=r.result;
                    }
                }
            });
            vm.showList = false;
        },
        del: function () {
            var uids = getSelectedRows();
            if(uids == null){
                return ;
            }
            var flag=confirm('确定要删除选中的记录？');
            if(flag){
                $.ajax({
                    type: "POST",
                    url: baseURL + "/user/deleteByUid.do",
                    contentType: "application/json",
                    data: JSON.stringify(uids),
                    success: function(r){
                        if(r.code == 0){
                        	 vm.reload();
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            }
        },
        saveOrUpdate: function () {
        	/*var user={};
        	user.uid=$("#uid").val();
        	user.loginname=$("#loginname").val();
        	user.gender=$("#gender").val();
        	user.address=$("#address").val();
        	user.phone=$("#phone").val();
        	user.businessType=$("#businessType").val();
        	user.remark=$("#remark").val();*/
        	//var user=vm.user;
            $.ajax({
                type: "POST",
                url: baseURL + 'user/updateUserInfo.do',
                contentType: "application/json",
                data: JSON.stringify(vm.user),
                success: function(r){
                    if(r.code === 0){
                    	confirm('操作成功', function(){
                        	vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'loginname': vm.loginname},
                page:page
            }).trigger("reloadGrid");
        }
    }
});